<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        .container {
            background: rgba(255, 255, 255, 0.1);
            padding: 20px;
            border-radius: 10px;
            backdrop-filter: blur(10px);
        }
        button {
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 10px 20px;
            margin: 5px;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        .result {
            background: rgba(0, 0, 0, 0.2);
            padding: 15px;
            margin: 10px 0;
            border-radius: 5px;
            white-space: pre-wrap;
            max-height: 400px;
            overflow-y: auto;
        }
        .status {
            padding: 10px;
            margin: 10px 0;
            border-radius: 5px;
        }
        .success { background: rgba(0, 255, 0, 0.2); }
        .error { background: rgba(255, 0, 0, 0.2); }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔥 热点追踪API测试</h1>
        
        <div>
            <h3>API连接测试</h3>
            <button onclick="testHealth()">健康检查</button>
            <button onclick="testAllData()">获取所有数据</button>
            <button onclick="testWeiboData()">微博数据</button>
            <button onclick="testDouyinData()">抖音数据</button>
        </div>
        
        <div id="status" class="status"></div>
        <div id="result" class="result"></div>
    </div>

    <script>
        const API_BASE = 'http://localhost:3001/api';
        
        function updateStatus(message, isError = false) {
            const status = document.getElementById('status');
            status.textContent = message;
            status.className = `status ${isError ? 'error' : 'success'}`;
        }
        
        function updateResult(data) {
            document.getElementById('result').textContent = 
                typeof data === 'string' ? data : JSON.stringify(data, null, 2);
        }
        
        async function testHealth() {
            updateStatus('正在检查API健康状态...');
            try {
                const response = await fetch(`${API_BASE}/health`);
                const data = await response.json();
                updateStatus('✅ API健康检查成功');
                updateResult(data);
            } catch (error) {
                updateStatus(`❌ API健康检查失败: ${error.message}`, true);
                updateResult(error.message);
            }
        }
        
        async function testAllData() {
            updateStatus('正在获取所有热搜数据...');
            try {
                const response = await fetch(`${API_BASE}/hotspots`);
                const data = await response.json();
                updateStatus(`✅ 成功获取数据，共${data.data?.length || 0}条`);
                updateResult(data);
            } catch (error) {
                updateStatus(`❌ 获取数据失败: ${error.message}`, true);
                updateResult(error.message);
            }
        }
        
        async function testWeiboData() {
            updateStatus('正在获取微博热搜数据...');
            try {
                const response = await fetch(`${API_BASE}/hotspots?platform=weibo`);
                const data = await response.json();
                updateStatus(`✅ 成功获取微博数据，共${data.data?.length || 0}条`);
                updateResult(data);
            } catch (error) {
                updateStatus(`❌ 获取微博数据失败: ${error.message}`, true);
                updateResult(error.message);
            }
        }
        
        async function testDouyinData() {
            updateStatus('正在获取抖音热搜数据...');
            try {
                const response = await fetch(`${API_BASE}/hotspots?platform=douyin`);
                const data = await response.json();
                updateStatus(`✅ 成功获取抖音数据，共${data.data?.length || 0}条`);
                updateResult(data);
            } catch (error) {
                updateStatus(`❌ 获取抖音数据失败: ${error.message}`, true);
                updateResult(error.message);
            }
        }
        
        // 页面加载时自动进行健康检查
        window.onload = () => {
            testHealth();
        };
    </script>
</body>
</html>